﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Services.Exceptions;

namespace HiTeachers.Services
{
    public class LoggerEmailServices
    {
        //private static readonly ILoggerEmailDao LoggerEmailDao = NHibernateDaoFactory.Instance.GetDao<ILoggerEmailDao>("loggerEmail");
        private static readonly ILoggerEmailDao LoggerEmailDao = NHibernateDaoFactory.Instance.GetLoggerEmailDao();

        public static LoggerEmail GetById(Guid itemId)
        {
            return LoggerEmailDao.GetById(itemId, false);
        }

        public static IList<LoggerEmail> GetSubcribeEmails()
        {
            return LoggerEmailDao.GetSubcribeEmails();
        }

        public static bool IsExistEmail(string emailAddress, EmailType emailType)
        {
            return LoggerEmailDao.IsExistEmail(emailAddress, emailType);
        }

        public static LoggerEmail SaveOrUpdate(LoggerEmail loggerEmail)
        {
            if (!loggerEmail.ID.Equals(Guid.Empty))
                return LoggerEmailDao.SaveOrUpdate(loggerEmail);

            if (IsExistEmail(loggerEmail.Email, loggerEmail.Type))
                throw new ObjectAlreadyExistException("Your email is already exist!");

            return LoggerEmailDao.Save(loggerEmail);
        }

        public static void Delete(Guid itId)
        {
            var loggerEmail = LoggerEmailDao.GetById(itId, false);
            if (loggerEmail != null) LoggerEmailDao.Delete(loggerEmail);
        }

        #region Selection Methods for DataGrid
        public static int TotalOfLoggerEmail(string eType)
        {
            var emailType = (EmailType)Enum.Parse(typeof(EmailType), eType);
            return LoggerEmailDao.TotalNumberOfLoggerEmail(emailType);
        }

        public static IList<LoggerEmail> GetLoggerEmails(string eType)
        {
            var emailType = (EmailType)Enum.Parse(typeof(EmailType), eType);
            return LoggerEmailDao.GetLoggerEmails(emailType);
        }

        public static IList<LoggerEmail> GetLoggerEmails(string eType, int maximumRows, int startRowIndex)
        {
            var emailType = (EmailType)Enum.Parse(typeof(EmailType), eType);
            return LoggerEmailDao.GetLoggerEmails(emailType, maximumRows, startRowIndex);
        }
        #endregion
    }
}
